package org.jeecgframework.web.material.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.material.entity.PProductEntity;
import org.jeecgframework.web.material.entity.PartTemplateEntity;
import org.jeecgframework.web.material.entity.ProductTemplateEntity;
import org.jeecgframework.web.material.service.PartTemplateServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Description: 配件模板
 * @date 2015-08-19 09:51:44
 * @version V1.0
 * 
 */
@Scope("prototype")
@Controller
@RequestMapping("/partTemplateController")
public class PartTemplateController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(PartTemplateController.class);

	@Autowired
	private PartTemplateServiceI partTemplateService;
	@Autowired
	private SystemService systemService;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "partTemplate")
	public ModelAndView partTemplate(HttpServletRequest request) {
		return new ModelAndView("material/partTemplate/partTemplateList");
	}

	/**
	 * 配件模板新增或编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAddOrUpdate")
	public ModelAndView goAdd(PartTemplateEntity partTemplate,
			HttpServletRequest req) {
		if (StringUtil.isNotEmpty(partTemplate.getId())) {
			partTemplate = partTemplateService.getEntity(
					PartTemplateEntity.class, partTemplate.getId());
			
			req.setAttribute("partTemplatePage", partTemplate);
		}
		return new ModelAndView("material/partTemplate/partTemplate");
	}

	/**
	 * 
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(PartTemplateEntity partTemplate,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(PartTemplateEntity.class, dataGrid);
		// 查询条件组装器

		try {
			// 自定义追加查询条件
			TSUser user = ResourceUtil.getSessionUserName();
			cq.createAlias("companyId", "companyId");
			cq.eq("companyId.id", user.getCompany().getId());
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.partTemplateService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 添加配件模板
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAddOrUpdate")
	@ResponseBody
	public AjaxJson doAdd(PartTemplateEntity partTemplate,
			HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isEmpty(partTemplate.getId())) {
			message = "配件模板添加成功";
			try {
				partTemplateService.saveMain(partTemplate);
				systemService.addLog(message, Globals.Log_Type_INSERT,
						Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "配件模板添加失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);

		} else {
			message = "配件模板修改成功";
			try {
				partTemplateService.updateMain(partTemplate);
				systemService.addLog(message, Globals.Log_Type_UPDATE,
						Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "配件模板修改失败";
				throw new BusinessException(e.getMessage());
			}
			j.setMsg(message);
		}

		return j;
	}

	/**
	 * 删除配件模板
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(PartTemplateEntity partTemplate,
			HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		partTemplate = systemService.getEntity(PartTemplateEntity.class,
				partTemplate.getId());
		message = "配件模板删除成功";
		try {
			partTemplateService.deleteMain(partTemplate);
			systemService.addLog(message, Globals.Log_Type_DEL,
					Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "配件模板删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 查询模板下的产品信息
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagridProduct")
	public void datagridProduct(HttpServletRequest request,
			HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ProductTemplateEntity.class,
				dataGrid);

		try {
			String ptid = request.getParameter("ptid");
			cq.createAlias("templateId", "templateId");
			cq.eq("templateId.id", ptid);
			// 自定义追加查询条件
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.partTemplateService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 增加和编辑功能下显示的产品信息
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagridAddProduct")
	public void datagridAddProduct(HttpServletRequest request,
			HttpServletResponse response, DataGrid dataGrid) {
		String ptid = request.getParameter("ptid");
		String productIds = request.getParameter("productIds");

		List<PProductEntity> listProduct = new ArrayList<PProductEntity>();
		try {

			if (StringUtil.isNotEmpty(productIds)) {
				String[] ids = productIds.split(",");
				CriteriaQuery cqP = new CriteriaQuery(PProductEntity.class);
				cqP.in("id", ids);
				cqP.add();
				listProduct = this.partTemplateService.getListByCriteriaQuery(
						cqP, false);
			}

			List<ProductTemplateEntity> listPTemplate = new ArrayList<ProductTemplateEntity>();
			CriteriaQuery cqT = new CriteriaQuery(ProductTemplateEntity.class);
			cqT.createAlias("templateId", "templateId");
			cqT.eq("templateId.id", ptid);
			cqT.add();
			listPTemplate = this.partTemplateService.getListByCriteriaQuery(
					cqT, false);
			// 合并已有的和ids查询出来的
			for (ProductTemplateEntity pt : listPTemplate) {
				PProductEntity product = pt.getProductId();
				listProduct.add(product);
			}

			// 自定义追加查询条件
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		dataGrid.setResults(listProduct);
		dataGrid.setTotal(listProduct.size());
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 跳转到选择产品
	 * 
	 * @return
	 */
	@RequestMapping(params = "goSelectProduct")
	public ModelAndView goSelectProduct(PartTemplateEntity partTemplate,
			HttpServletRequest req) {
		
		return new ModelAndView("material/common/selectProduct");
	}
	
	/**
	 * 跳转到模板产品列表
	 * 
	 * @return
	 */
	@RequestMapping(params = "goPTemplateList")
	public ModelAndView goPTemplateList(PartTemplateEntity partTemplate,
			HttpServletRequest req) {
		if (StringUtil.isNotEmpty(partTemplate.getId())) {
			partTemplate = partTemplateService.getEntity(
					PartTemplateEntity.class, partTemplate.getId());
			
			req.setAttribute("productTemplateList", partTemplate.getListPT());
		}
		return new ModelAndView("material/partTemplate/PTemplateList");
	}

}
